/**
************************************************************
* @file         motor.c
* @brief        motor driver
* @author       Gizwits
* @date         2018-07-19
* @version      V03030000
* @copyright    Gizwits
*
* @note         机智云.只为智能硬件而生
*               Gizwits Smart Cloud  for Smart Products
*               链接|增值ֵ|开放|中立|安全|自有|自由|生态
*               www.gizwits.com
*
***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "qapi/qapi.h"
#include "qapi/qapi_status.h"

#include "qapi/qapi_gpioint.h"
#include "qapi/qapi_tlmm.h"
#include "qapi/qapi_types.h"
#include "qapi/qapi_fs.h"

#include "motor.h"

#define FAIL    -1
#define SUCCESS  0

enum modem_pin_value {
    MODEM_PIN_11 = 23,
    MODEM_PIN_12 = 20,
    MODEM_PIN_13 = 21,
    MODEM_PIN_14 = 22,
    MODEM_PIN_37 = 2,
    MODEM_PIN_38 = 3,
    MODEM_PIN_48 = 15,
    MODEM_PIN_52 = 50,
    MODEM_PIN_66 = 74,
    MODEM_PIN_67 = 10,
    MODEM_PIN_68 = 11
};

uint32_t motor_pin = MODEM_PIN_48;
qapi_TLMM_Config_t motor_config;
qapi_GPIO_ID_t     motorGPIO;

void motorSetValue(uint8_t value)
{
    if (0 == value)
    {
        qapi_TLMM_Drive_Gpio(motorGPIO, motor_pin, QAPI_GPIO_LOW_VALUE_E);
    }
    else
    {
        qapi_TLMM_Drive_Gpio(motorGPIO, motor_pin, QAPI_GPIO_HIGH_VALUE_E);
    }

    return;
}

void motorInit(void)
{
    qapi_Status_t      status;

    motor_config.pin = motor_pin;
    motor_config.func = 0;
    motor_config.dir = QAPI_GPIO_OUTPUT_E;
    motor_config.pull = QAPI_GPIO_PULL_DOWN_E;
    motor_config.drive =  QAPI_GPIO_2MA_E;

    status = qapi_TLMM_Get_Gpio_ID( &motor_config, &motorGPIO);
    if (status == QAPI_OK)
    {
        status = qapi_TLMM_Config_Gpio(motorGPIO, &motor_config);
        if (status != QAPI_OK)
        {
            iofPrintf("motorInit ERROR: %d\n", status );
        }
    }
    else
    {
        iofPrintf("motorInit ERROR: %d\n", status);
    }
    
    motorSetValue(1);

    return;
}

void motorExit(void)
{
    qapi_Status_t      status;

    status = qapi_TLMM_Release_Gpio_ID( &motor_config, motorGPIO);
    if (status != QAPI_OK)
    {
        iofPrintf("qapi_TLMM_Release_Gpio_ID ERROR: %d\n", status );
    }

    return;
}